********************
*** 3_PART THREE ***
********************
cd "~"
use 1_p1_cntry.dta, clear
keep env_norm imueclt_norm grhom_norm trstun_norm cc_index anweight land year

levelsof land, local(countries)
levelsof year, local(years)

// Inter-item correlations
gen env_itrest_A = .
gen im_itrest_A  = .
gen hom_itrest_A = .
gen un_itrest_A  = .

foreach l of local countries {
    foreach i of local years {
        quietly {
			capture {
		alphawgt env_norm-trstun_norm if land==`l' & year==`i' [aw=anweight], item
		matrix m = r(ItemRestCorr)

		replace env_itrest_A = m[1,1] if land==`l' & year==`i'
		replace im_itrest_A  = m[1,2] if land==`l' & year==`i'
		replace hom_itrest_A = m[1,3] if land==`l' & year==`i'
		replace un_itrest_A  = m[1,4] if land==`l' & year==`i'
			}
		}
	}
}

gen env_itrest_B = .
gen im_itrest_B  = .
gen hom_itrest_B = .
gen un_itrest_B  = .
gen cc_itrest_B  = .

foreach l of local countries {
    foreach i of local years {
        if `i' >= 2016 {
            quietly {
				capture{
		alphawgt env_norm-cc_index if land==`l' & year==`i' [aw=anweight], item
		matrix m = r(ItemRestCorr)

		replace env_itrest_B = m[1,1] if land==`l' & year==`i'
		replace im_itrest_B  = m[1,2] if land==`l' & year==`i'
		replace hom_itrest_B = m[1,3] if land==`l' & year==`i'
		replace un_itrest_B  = m[1,4] if land==`l' & year==`i'
		replace cc_itrest_B  = m[1,5] if land==`l' & year==`i'
				}
			}
		}
	}
}

// Factor analyses
gen env_load_A = .
gen im_load_A  = .
gen hom_load_A = .
gen un_load_A  = .

foreach l of local countries {
    foreach i of local years {
		quietly {
			capture {

	factor env_norm imueclt_norm grhom_norm trstun_norm if land==`l' & year==`i' [aw=anweight], factor(1)
	matrix mL = e(L)
	
		replace env_load_A = mL[1,1] if land==`l' & year==`i'
		replace im_load_A  = mL[2,1] if land==`l' & year==`i'
		replace hom_load_A = mL[3,1] if land==`l' & year==`i'
		replace un_load_A  = mL[4,1] if land==`l' & year==`i'
			}
		}
	}
}

gen env_load_B = .
gen im_load_B  = .
gen hom_load_B = .
gen un_load_B  = .
gen cc_load_B  = .

foreach l of local countries {
    foreach i of local years {
        if `i' >= 2016 {
            quietly {
				capture{

	factor env_norm imueclt_norm grhom_norm trstun_norm cc_index if land==`l' & year==`i' [aw=anweight], factor(1)
	matrix mL = e(L)
	
		replace env_load_B = mL[1,1] if land==`l' & year==`i'
		replace im_load_B  = mL[2,1] if land==`l' & year==`i'
		replace hom_load_B = mL[3,1] if land==`l' & year==`i'
		replace un_load_B  = mL[4,1] if land==`l' & year==`i'
		replace cc_load_B  = mL[5,1] if land==`l' & year==`i'
				}
			}
		}
	}
}

save 3_p3_imp.dta, replace

collapse *_itrest_* *_load_*, by(land year)

levelsof year, local(years)

foreach var of varlist *_itrest* *_load_* {
	gen `var'_mean = .
	gen `var'_sd = .
	gen `var'_mmsd = .
	gen `var'_mpsd = .
	
	foreach i of local years {
		capture {
	sum `var' if year == `i'
		replace `var'_mean = r(mean) if year == `i'
		replace `var'_sd = r(sd) if year == `i'
	}
	
	replace `var'_mmsd = `var'_mean - `var'_sd
	replace `var'_mpsd = `var'_mean + `var'_sd
}
}

save 3_part_three.dta, replace

colorpalette cblind
return list

colorpalette cblind, select(3 4 5 8 9) globals

** Figure 6
	twoway connected env_itrest_A_mean im_itrest_A_mean hom_itrest_A_mean un_itrest_A_mean year, sort ///
		msymbol(X Sh Oh Dh) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue) lp(dash "---..." longdash shortdash_dot) lwidth(vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") size(medsmall) symplacement(center) j(center)) ///
		title("Item-rest" "correlations" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2002(20)2022, nogrid labsize(medsmall)) xmlab(2004(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(Aa_itrest, replace)

	twoway connected env_load_A_mean im_load_A_mean hom_load_A_mean un_load_A_mean year, sort ///
		msymbol(X Sh Oh Dh) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue) lp(dash "---..." longdash shortdash_dot) lwidth(vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") size(medsmall) symplacement(center) j(center)) ///
		title("Factor" "loadings" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2002(20)2022, nogrid labsize(medsmall)) xmlab(2004(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(Aa_load, replace)

grc1leg Aa_itrest Aa_load, rows(1)

** Figure 7a
	twoway connected env_itrest_A_mean im_itrest_A_mean hom_itrest_A_mean un_itrest_A_mean year if year >= 2016, sort ///
		msymbol(X Sh Oh Dh) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue) lp(dash "---..." longdash shortdash_dot) lwidth(vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") size(medsmall) symplacement(center) j(center)) ///
		title("Belief System {it:A}" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2016(6)2022, nogrid labsize(medsmall)) xmlab(2018(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(A2_itrest, replace)
		
	twoway connected env_itrest_B_mean im_itrest_B_mean hom_itrest_B_mean un_itrest_B_mean cc_itrest_B_mean year if year >= 2016, sort ///
		msymbol(X Sh Oh Dh Th) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue $reddish_Purple) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue $reddish_Purple) lp(dash "---..." longdash shortdash_dot solid) lwidth(vthin vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") label(5 "Climate" "change") size(medsmall) symplacement(center) j(center)) ///
		title("Belief System {it:B}" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2016(6)2022, nogrid labsize(medsmall)) xmlab(2018(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(B_itrest, replace)
	
grc1leg A2_itrest B_itrest, legend(B_itrest) ycommon 

	ttest cc_itrest_B = im_itrest_B if year == 2016
	ttest cc_itrest_B = im_itrest_B if year == 2020
	ttest cc_itrest_B = im_itrest_B if year == 2022

	tab land if cc_itrest_B > im_itrest_B & year == 2016
	tab land if cc_itrest_B < im_itrest_B & year == 2016
	tab land if cc_itrest_B > im_itrest_B & year == 2020
	tab land if cc_itrest_B < im_itrest_B & year == 2020
	tab land if cc_itrest_B > im_itrest_B & year == 2022
	tab land if cc_itrest_B < im_itrest_B & year == 2022

	rowranks env_itrest_B im_itrest_B hom_itrest_B un_itrest_B cc_itrest_B, gen(env_itrest_rank im_itrest_rank hom_itrest_rank un_itrest_rank cc_itrest_rank)
	tab cc_itrest_rank if year == 2022
	tab land if cc_itrest_rank != 5 & year == 2022

	by land: tabstat *_itrest_rank if inlist(land,4,6,17,19,21,22,24,25,32,35,36) & year == 2022

** Figure 7b
	twoway connected env_load_A_mean im_load_A_mean hom_load_A_mean un_load_A_mean year if year >= 2016, sort ///
		msymbol(X Sh Oh Dh) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue) lp(dash "---..." longdash shortdash_dot) lwidth(vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") size(medsmall) symplacement(center) j(center)) ///
		title("Belief System {it:A}" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2016(6)2022, nogrid labsize(medsmall)) xmlab(2018(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(A2_load, replace)
		
	twoway connected env_load_B_mean im_load_B_mean hom_load_B_mean un_load_B_mean cc_load_B_mean year if year >= 2016, sort ///
		msymbol(X Sh Oh Dh Th) msize(large medlarge medlarge medlarge medlarge) mc($bluish_Green $Vermillion $Orange $Sky_Blue $reddish_Purple) ///
		lc($bluish_Green $Vermillion $Orange $Sky_Blue $reddish_Purple) lp(dash "---..." longdash shortdash_dot solid) lwidth(vthin vthin vthin vthin vthin) ///
		legend(pos(6) rows(1) stack label(1 "Environmental" "protection") label(2 "Immigration") ///
		label(3 "Homosexuality") label(4 "International" "cooperation") label(5 "Climate" "change") size(medsmall) symplacement(center) j(center)) ///
		title("Belief System {it:B}" " ", size(medlarge)) plotregion(margin(1+2 1 1 1+2) sty(outline)) yscale(lstyle(none)) xscale(lstyle(none)) ///
		xlab(2016(6)2022, nogrid labsize(medsmall)) xmlab(2018(2)2020, nolab) ylab(, nogrid labsize(medsmall)) xtitle(" ") ytitle("") name(B_load, replace)
	
grc1leg A2_load B_load, legend(B_load) ycommon 

	ttest cc_load_B = im_load_B if year == 2016
	ttest cc_load_B = im_load_B if year == 2020
	ttest cc_load_B = im_load_B if year == 2022

	tab land if cc_load_B > im_load_B & year == 2016
	tab land if cc_load_B < im_load_B & year == 2016
	tab land if cc_load_B > im_load_B & year == 2020
	tab land if cc_load_B < im_load_B & year == 2020
	tab land if cc_load_B > im_load_B & year == 2022
	tab land if cc_load_B < im_load_B & year == 2022

	rowranks env_load_B im_load_B hom_load_B un_load_B cc_load_B, gen(env_load_rank im_load_rank hom_load_rank un_load_rank cc_load_rank)
	tab cc_load_rank if year == 2022
	tab land if cc_load_rank != 5 & year == 2022

	by land: tabstat *_load_rank if inlist(land,4,17,19,21,22,24,25,35,36) & year == 2022

		// APPENDIX	
		// Table A4
		use 1_p1_cntry.dta, clear
		keep land year anweight ///
			env_norm imueclt_norm grhom_norm trstun_norm ///
			ccwor_norm ccpers_norm cchum_norm
			
		levelsof land, local(countries)
		levelsof year, local(years)

		// Item-rest correlations
		gen env_itrest_B = .
		gen im_itrest_B  = .
		gen hom_itrest_B = .
		gen un_itrest_B  = .

		gen ccwor_itrest_B  = .
		gen ccpers_itrest_B  = .
		gen cchum_itrest_B  = .

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{
				alphawgt env_norm imueclt_norm grhom_norm trstun_norm ccwor_norm if land==`l' & year==`i' [aw=anweight], item
				matrix m = r(ItemRestCorr)

				replace env_itrest_B = m[1,1] if land==`l' & year==`i'
				replace im_itrest_B  = m[1,2] if land==`l' & year==`i'
				replace hom_itrest_B = m[1,3] if land==`l' & year==`i'
				replace un_itrest_B  = m[1,4] if land==`l' & year==`i'
				replace ccwor_itrest_B  = m[1,5] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{
				alphawgt env_norm imueclt_norm grhom_norm trstun_norm ccpers_norm if land==`l' & year==`i' [aw=anweight], item
				matrix m = r(ItemRestCorr)

				replace env_itrest_B = m[1,1] if land==`l' & year==`i'
				replace im_itrest_B  = m[1,2] if land==`l' & year==`i'
				replace hom_itrest_B = m[1,3] if land==`l' & year==`i'
				replace un_itrest_B  = m[1,4] if land==`l' & year==`i'
				replace ccpers_itrest_B  = m[1,5] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{
				alphawgt env_norm imueclt_norm grhom_norm trstun_norm cchum_norm if land==`l' & year==`i' [aw=anweight], item
				matrix m = r(ItemRestCorr)

				replace env_itrest_B = m[1,1] if land==`l' & year==`i'
				replace im_itrest_B  = m[1,2] if land==`l' & year==`i'
				replace hom_itrest_B = m[1,3] if land==`l' & year==`i'
				replace un_itrest_B  = m[1,4] if land==`l' & year==`i'
				replace cchum_itrest_B  = m[1,5] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		// Factor analyses
		gen env_load_B = .
		gen im_load_B  = .
		gen hom_load_B = .
		gen un_load_B  = .

		gen ccwor_load_B  = .
		gen ccpers_load_B  = .
		gen cchum_load_B  = .

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{

			factor env_norm imueclt_norm grhom_norm trstun_norm ccwor_norm if land==`l' & year==`i' [aw=anweight], factor(1)
			matrix mL = e(L)
			
				replace env_load_B = mL[1,1] if land==`l' & year==`i'
				replace im_load_B  = mL[2,1] if land==`l' & year==`i'
				replace hom_load_B = mL[3,1] if land==`l' & year==`i'
				replace un_load_B  = mL[4,1] if land==`l' & year==`i'
				replace ccwor_load_B  = mL[5,1] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{

			factor env_norm imueclt_norm grhom_norm trstun_norm ccpers_norm if land==`l' & year==`i' [aw=anweight], factor(1)
			matrix mL = e(L)
			
				replace env_load_B = mL[1,1] if land==`l' & year==`i'
				replace im_load_B  = mL[2,1] if land==`l' & year==`i'
				replace hom_load_B = mL[3,1] if land==`l' & year==`i'
				replace un_load_B  = mL[4,1] if land==`l' & year==`i'
				replace ccpers_load_B  = mL[5,1] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		foreach l of local countries {
			foreach i of local years {
				if `i' >= 2016 {
					quietly {
						capture{

			factor env_norm imueclt_norm grhom_norm trstun_norm cchum_norm if land==`l' & year==`i' [aw=anweight], factor(1)
			matrix mL = e(L)
			
				replace env_load_B = mL[1,1] if land==`l' & year==`i'
				replace im_load_B  = mL[2,1] if land==`l' & year==`i'
				replace hom_load_B = mL[3,1] if land==`l' & year==`i'
				replace un_load_B  = mL[4,1] if land==`l' & year==`i'
				replace cchum_load_B  = mL[5,1] if land==`l' & year==`i'
						}
					}
				}
			}
		}

		save 3_p3_imp_comp.dta, replace
		
		collapse *_itrest_* *_load_*, by(land year)

		levelsof year, local(years)

		foreach var of varlist *_itrest* *_load_* {
			gen `var'_mean = .
			gen `var'_sd = .
			gen `var'_mmsd = .
			gen `var'_mpsd = .
			
			foreach i of local years {
				capture {
			sum `var' if year == `i'
				replace `var'_mean = r(mean) if year == `i'
				replace `var'_sd = r(sd) if year == `i'
			}
			
			replace `var'_mmsd = `var'_mean - `var'_sd
			replace `var'_mpsd = `var'_mean + `var'_sd
		}
		}

		keep if inlist(year,2016,2020,2022)
		bysort year: tabstat env_itrest_B_mean im_itrest_B_mean hom_itrest_B_mean un_itrest_B_mean ///
			cchum_itrest_B_mean ccwor_itrest_B_mean ccpers_itrest_B_mean, format(%9.3f)

		bysort year: tabstat env_load_B_mean im_load_B_mean hom_load_B_mean un_load_B_mean ///
			cchum_load_B_mean ccwor_load_B_mean ccpers_load_B_mean, format(%9.3f)
